/*
 *	Source: http://www.yolinux.com/TUTORIALS/XML-Xerces-C.html
 */

#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <list>


#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#include "XMLParser.h"


using namespace xercesc;
using namespace std;


/**
 *  - Tests the access and availability of the XML configuration file.
 *  - Configures the xerces-c DOM parser.
 *  - Reads and extracts the pertinent information from the XML config file.
 * @param const char * configFile - filename
 * @param std::vector<Item*>& vecResult - information is stored here
 * @return void
 */
void XMLParser::readItemConfigFile(const char * configFile, vector<Item*>& vecResult)
{
	try
	{
		if (checkFile(configFile) == -1)
		{
			throw(std::runtime_error( "XML document not available" ));
		}
		m_ConfigFileParser->parse( configFile );
		DOMDocument* xmlDoc = m_ConfigFileParser->getDocument();
		DOMElement* elementRoot = xmlDoc->getDocumentElement();
		if( !elementRoot )
		{
			throw(std::runtime_error( "empty XML document" ));
		}
		if (!XMLString::equals(elementRoot->getTagName(), m_TAG_root))
		{
			throw(std::runtime_error( "Bad root tag for this config file." ));
		}
		DOMNodeList* childrens = elementRoot->getElementsByTagName(m_TAG_ApplicationSettings);
		const  XMLSize_t nodeCount = childrens->getLength();
		for( XMLSize_t i = 0; i < nodeCount; ++i )
		{
			DOMElement* currentElement = dynamic_cast< xercesc::DOMElement* >(childrens->item(i));
			if (m_ATTR_Options.size() == XMLPARSER_NUMOPTIONS_ITEMCONFIG)
			{
				Item* item = new Item;
				const XMLCh* xmlch_OptionA = currentElement->getAttribute(m_ATTR_Options[0]);
				const XMLCh* xmlch_OptionB = currentElement->getAttribute(m_ATTR_Options[1]);
				const XMLCh* xmlch_OptionC = currentElement->getAttribute(m_ATTR_Options[2]);
				const XMLCh* xmlch_OptionD = currentElement->getAttribute(m_ATTR_Options[3]);
				const XMLCh* xmlch_OptionE = currentElement->getAttribute(m_ATTR_Options[4]);
				const XMLCh* xmlch_OptionF = currentElement->getAttribute(m_ATTR_Options[5]);
				const XMLCh* xmlch_OptionG = currentElement->getAttribute(m_ATTR_Options[6]);
				const XMLCh* xmlch_OptionH = currentElement->getAttribute(m_ATTR_Options[7]);

				item->setName(XMLString::transcode(xmlch_OptionA));
				item->setDesc(XMLString::transcode(xmlch_OptionB));
				item->setType(atoi(XMLString::transcode(xmlch_OptionC)));
				item->setPrice(atoi(XMLString::transcode(xmlch_OptionD)));
				switch(item->getType())
				{
				case ITEM_TYPE_ARMOR:
					item->setArmorFactor(atoi(XMLString::transcode(xmlch_OptionE)));
					break;
				case ITEM_TYPE_ONEHANDEDWEAPON:
				case ITEM_TYPE_TWOHANDEDWEAPON:
					item->setBypassRoll(atoi(XMLString::transcode(xmlch_OptionF)),
										atoi(XMLString::transcode(xmlch_OptionG)));
										item->setDamage(atoi(XMLString::transcode(xmlch_OptionH)));
					break;
				default:
					break;
				}
				vecResult.push_back(item);
			}
		}
	}
	catch( xercesc::XMLException& e )
	{
		char* message = xercesc::XMLString::transcode( e.getMessage() );
		ostringstream errBuf;
		errBuf << "Error parsing file: " << message << flush;
		XMLString::release( &message );
	}
}
